package com.appspot.didit.client.workitem;

import java.util.Date;

import com.appspot.didit.client.tag.*;
import com.appspot.didit.client.util.Util;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.datepicker.client.DateBox;

public class WorkitemExportView extends Composite implements WorkitemExportPresenter.View {
	
	public final DateBox dateFrom, dateTo;
	
	public final TextBox tag;
	
	public final RadioButton[] rbs;
	
	public final Button exportButton;
	
    RadioButton[] daterange;
    RadioButton[] projectrange;
    VerticalPanel vp;
    
    public WorkitemExportView() {
    	
    	VerticalPanel vp = new VerticalPanel();
        
    	daterange = new RadioButton[2];
        FlexTable timeranges = new FlexTable();
        daterange[0] = new RadioButton("dateRange", "von");
        daterange[0].setValue(true);
        timeranges.setWidget(0, 0, daterange[0]);
        dateFrom = new DateBox();
        final Date now = new Date();
        dateFrom.setValue(now);
		dateFrom.setFormat(new DateBox.DefaultFormat(Util.completeDate));
		dateFrom.addValueChangeHandler(new ValueChangeHandler<Date>() {
			public void onValueChange(ValueChangeEvent<Date> event) {
				daterange[0].setValue(true);
				if(!dateTo.getValue().after(dateFrom.getValue())) {
					MessageBox.alert("", "Die ausgewählte Datum ist nicht gültig", null);
					dateFrom.setValue(dateTo.getValue());
				}
			}
        });
		timeranges.setWidget(0, 1, dateFrom);
		timeranges.setWidget(1, 0, new HTML("bis"));
        dateTo = new DateBox();
		dateTo.setValue(now);
		dateTo.setFormat(new DateBox.DefaultFormat(Util.completeDate));
		dateTo.addValueChangeHandler(new ValueChangeHandler<Date>() {
			public void onValueChange(ValueChangeEvent<Date> event) {
				daterange[0].setValue(true);
				if(!dateTo.getValue().after(dateFrom.getValue())) {
					MessageBox.alert("", "Die ausgewählte Datum ist nicht gültig", null);
					dateTo.setValue(dateFrom.getValue());
				}
				if(dateTo.getValue().after(now)) {
					MessageBox.alert("", "Die ausgewählte Datum sollte nicht in der Zukunft sein.", null);
					dateTo.setValue(now);
				}
			}
        });
		timeranges.setWidget(1, 1, dateTo);
		daterange[1] = new RadioButton("dateRange", "");
        final ListBox dropBox = new ListBox(false);
        dropBox.addItem("heute", "1");
        dropBox.addItem("gestern", "2");
        dropBox.addItem("letzte 7 Tage", "7");
        dropBox.addItem("letzte 30 Tage", "30");
        dropBox.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				daterange[1].setValue(true);
				switch(dropBox.getSelectedIndex()) {
				case 0:
					dateFrom.setValue(now);
					dateTo.setValue(now);
					break;
				case 1:
					dateFrom.setValue(new Date(now.getTime()-Util.MilisecondsPerDay));
					dateTo.setValue(dateFrom.getValue());
					break;
				case 2:
					dateFrom.setValue(new Date(now.getTime() - 7 * Util.MilisecondsPerDay));
					dateTo.setValue(now);
					break;
				case 3:
					dateFrom.setValue(new Date(now.getTime() - 30 * Util.MilisecondsPerDay));
					dateTo.setValue(now);
					break;
				default:
					break;
				}
			}
        });
		timeranges.setWidget(2, 0, daterange[1]);
		timeranges.setWidget(2, 1, dropBox);
		
		
		
		FlexTable projectranges = new FlexTable();
		projectrange = new RadioButton[2];
		projectrange[0] = new RadioButton("projectRange", "");
		projectrange[0].setValue(true);
		projectranges.setWidget(0, 0, projectrange[0]);
		tag = new TextBox();
		tag.setReadOnly(true);
        tag.addFocusHandler(new FocusHandler() {
			public void onFocus(FocusEvent event) {
				projectrange[0].setValue(true);
				TagCloud tc = TagManager.getInstance().getTagCloud();
				if(tc != null && !tc.isShowing()) {
					tc.show(tag);
				}
			}
		});
        tag.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				tag.setStyleName("");
				
			}
		});
        tag.addKeyDownHandler(new KeyDownHandler() {
			@Override
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_TAB) {
					 TagCloud tc = TagManager.getInstance().getTagCloud();
						if(tc != null && tc.getPartner().equals(tag)) {
							tc.hide();
						}
				 }
			}
        	
        });
        tag.addValueChangeHandler(new ValueChangeHandler<String>() {

			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				projectrange[0].setValue(true);
			}
        	
        });
        projectranges.setWidget(0, 1, tag);
        projectrange[1] = new RadioButton("projectRange", "");
        projectrange[1].addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				if(event.getValue()) {
					tag.setValue(TagManager.getInstance().getAllProjectsAsString());
				}
			}
        });
        projectranges.setWidget(1, 0, projectrange[1]);
        projectranges.setWidget(1, 1, new HTML("ALLE Projekte"));
        
        exportButton = new Button("Exportieren");
        
        FlexTable layout = new FlexTable();

        // Add some standard form options
        layout.setWidget(0, 0, new HTML("Zeitumfang:"));
		layout.setWidget(0, 1, timeranges);
		layout.setWidget(1, 0, new HTML("<hr/>"));
		layout.setWidget(1, 1, new HTML("<hr/>"));
		layout.setWidget(2, 0, new HTML("Relevante Projekte:"));
		layout.setWidget(2, 1, projectranges);
		layout.setWidget(3, 0, new HTML("<hr/>"));
		layout.setWidget(3, 1, new HTML("<hr/>"));
        layout.setWidget(4, 0, new HTML("Template:"));
        FlexTable templatelist = new FlexTable();
        rbs = new RadioButton[4];
        String[] labels = {"Meine Workitems ohne Projektangabe", 
        		"Meine Workitems von den Projekten", 
        		"Alle Workitems von einem Projekt",
        		"Stundenzettel"
        };
        for(int i = 0; i < 4; i++) {
        	rbs[i] = new RadioButton("chartlist", labels[i]);
        	templatelist.setWidget(i, 0, rbs[i]);
        }
        
        layout.setWidget(4, 1, templatelist);
        
        //layout.setWidget(3, 1, exportButton);
        //layout.setBorderWidth(1);
        vp.add(layout);
        vp.add(exportButton);
        initWidget(vp);
    }

    public Widget getWidget() {
            return this;
    }

	public void reset() {
		
	}
	
	public HasClickHandlers getExportClickHandlers() {
		return exportButton;
	}

	public HasClickHandlers[] getRaidoButtons() {
		return rbs;
	}
	
	public HasValue<Date> getDateFrom() {
		return dateFrom;
	}
	
	public HasValue<Date> getDateTo() {
		return dateTo;
	}

	public HasValue<String> getTag() {
		return tag;
	}

}
